/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import DAL.Album;
import DAL.DAL;
import DAL.Imageupload;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 *
 * @author baopx
 */
public class ImageUtil {

    private static int ID = -1;

    public static void SaveImage(byte[] data, String formatImage, DAL d,Album album) {
        try {
            if (ID == -1) {
                ID = d.GetID();
            }
            String fileName = "ImagesUpload/" + ID + "." + formatImage;
            Image image = Toolkit.getDefaultToolkit().createImage(data);
            final BufferedImage bufferedImage = ToBufferedImage(image);
            ImageIO.write(bufferedImage, formatImage, new File(fileName));
            Imageupload imageupload = new Imageupload();
            imageupload.setName(ID+"." + formatImage);
            imageupload.setNameFile(fileName);
            imageupload.setDateUpload( new Date());
            imageupload.setAlbumId(album.getId());
            imageupload.setIsDelete(false);
            d.Insert(imageupload);
            ID++;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static BufferedImage ConvertImageToBufferedImage(Image image) {
        BufferedImage bimg = null;
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        int[] pixels = new int[w * h];
        PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
        try {
            pg.grabPixels();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }

        bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        bimg.setRGB(0, 0, w, h, pixels, 0, w);
        return bimg;
    }

    private static BufferedImage ToBufferedImage(Image image) {
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }
        image = new ImageIcon(image).getImage();
        boolean hasAlpha = HasAlpha(image);
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            int transparency = Transparency.OPAQUE;
            if (hasAlpha == true) {
                transparency = Transparency.BITMASK;
            }
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {
        }
        if (bimage == null) {
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha == true) {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }
        Graphics g = bimage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bimage;
    }

    private static boolean HasAlpha(Image image) {
        if (image instanceof BufferedImage) {
            return ((BufferedImage) image).getColorModel().hasAlpha();
        }
        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }
        return pg.getColorModel().hasAlpha();
    }
}
